//
//  DervativeAreaController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/9/24.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "DervativeAreaController.h"
#import "BusinessModel.h"
#import "BusinessViewCell.h"
#import "BusinessDetailsController.h"

@interface DervativeAreaController ()
{
    TopPullRefreshAnaimation *gifHeader;
    MBProgressHUD *progressHUD;
    NSUInteger refreshIndex;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)NSMutableArray *areaArray;

@end

@implementation DervativeAreaController

static NSString *const DerivativeAreaCell = @"DerivativeAreaCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.rowHeight = HEIGHT/5;
    refreshIndex = 1;
    //上啦
    [self addPullUpdateData];
    //请求
    [self DerivativeAreaLoadData];
}

#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.tableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(DerivativeAreaLoadMoreData)];
    
}

- (void)DerivativeAreaLoadMoreData
{
    refreshIndex ++;
    [self DerivativeAreaLoadData];
    [self.tableView.mj_footer endRefreshing];
}


- (void)DerivativeAreaLoadData{
    
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    [noDrawImage removeFromSuperview];
    
    NSString *P = [NSString stringWithFormat:@"%ld",refreshIndex];
    
    NSDictionary *params = @{@"p":P,
                             };
    
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Business/weixinBusinessList" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        NSArray *arr = object[@"data"];
        if ([[object objectForKey:@"code"] isEqual:@(1)]) {
            for (NSDictionary *dict in arr) {
                BusinessModel *businessModel = [[BusinessModel alloc] initWithDictionary:dict error:nil];
                
                if (businessModel) {
                    [self.areaArray addObject:businessModel];
                } else{
                    //
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                
            }
            
        }else{
            
            
            if (refreshIndex == 1) {
                
               //暂无地址(图)
               noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noBussiness"]];
              [self.view addSubview:noDrawImage];

               [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/10);
                   make.width.mas_equalTo(WIDTH/2);
                   make.height.mas_equalTo(HEIGHT/4);
               }];
            }
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        [progressHUD hide:YES];
    }];
    
    
}





#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.areaArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    BusinessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DerivativeAreaCell];
    
    if (cell==nil) {
        
        cell = [[BusinessViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DerivativeAreaCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    BusinessModel *businessModel = self.areaArray[indexPath.row];
    [cell.shopsImg sd_setImageWithURL:[NSURL URLWithString:businessModel.sdpic] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    cell.shopsNameLabel.text = businessModel.title;
    cell.shopsDetailsLabel.text = businessModel.zycp;
    [cell.shopsAddress setTitle:businessModel.county forState:UIControlStateNormal];
    cell.shopsAddress .hidden = YES;
    
    cell.messageButton.hidden = YES;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BusinessDetailsController *detailVC = [[BusinessDetailsController alloc] init];
    BusinessModel *businessModel = self.areaArray[indexPath.row];
    detailVC.bussinessId = businessModel.replaceID;
    detailVC.isWeChatDetails = YES;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detailVC];
}



- (NSMutableArray *)areaArray
{
    if (!_areaArray) {
        _areaArray = [NSMutableArray array];
    }
    return _areaArray;
}


@end
